/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.server.modules.sys.web.mailaddr;

import com.server.common.config.Global;
import com.server.common.persistence.Page;
import com.server.common.utils.StringUtils;
import com.server.common.web.BaseController;
import com.server.modules.sys.entity.mailaddr.MailAddr;
import com.server.modules.sys.service.mailaddr.MailAddrService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 邮件列表Controller
 * @author 丁光彪
 * @version 2017-01-10
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/mailaddr/mailAddr")
public class MailAddrController extends BaseController {

	@Autowired
	private MailAddrService mailAddrService;
	
	@ModelAttribute
	public MailAddr get(@RequestParam(required=false) String id) {
		MailAddr entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = mailAddrService.get(id);
		}
		if (entity == null){
			entity = new MailAddr();
		}
		return entity;
	}
	
	@RequiresPermissions("sys:mailaddr:mailAddr:view")
	@RequestMapping(value = {"list", ""})
	public String list(MailAddr mailAddr, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<MailAddr> page = mailAddrService.findPage(new Page<MailAddr>(request, response), mailAddr); 
		model.addAttribute("page", page);
		model.addAttribute("mailAddr", mailAddr);
		return "modules/sys/mailaddr/mailAddrList";
	}

	@RequiresPermissions("sys:mailaddr:mailAddr:view")
	@RequestMapping(value = "form")
	public String form(MailAddr mailAddr, Model model) {
		model.addAttribute("mailAddr", mailAddr);
		return "modules/sys/mailaddr/mailAddrForm";
	}

	@RequiresPermissions("sys:mailaddr:mailAddr:edit")
	@RequestMapping(value = "save")
	public String save(MailAddr mailAddr, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, mailAddr)){
			return form(mailAddr, model);
		}
		mailAddrService.save(mailAddr);
		addMessage(redirectAttributes, "保存邮件列表成功");
		return "redirect:"+Global.getAdminPath()+"/sys/mailaddr/mailAddr/?repage";
	}
	
	@RequiresPermissions("sys:mailaddr:mailAddr:edit")
	@RequestMapping(value = "delete")
	public String delete(MailAddr mailAddr, RedirectAttributes redirectAttributes) {
		mailAddrService.delete(mailAddr);
		addMessage(redirectAttributes, "删除邮件列表成功");
		return "redirect:"+Global.getAdminPath()+"/sys/mailaddr/mailAddr/?repage";
	}

}